<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once BASE_PATH.'application/controllers/application.php';
class Users extends APP_Controller {

	public $public_methods = array('login', 'change_password');
	public $admin_methods = array('index', 'add', 'delete');


	public function index(){
		$this->load->model('User_model', 'users');
		$viewdata['users'] = $this->users->list_users();
		if($this->session->flashdata('message') ){
			$viewdata['message'] = $this->session->flashdata('message');
		}
		$this->load->view('users/list', $viewdata);
	}
	
	public function add(){
		$postdata = $this->input->post();
		//IF POST, Add user
		if($postdata){
			$this->load->model('User_model', 'user');
			if( $this->user->add($postdata) ){
				$this->session->set_flashdata('message', 'User saved');
				$this->redirect('users/');
			}else{
				$viewdata['message'] = 'User not saved';
				$viewdata['errors'] = $this->user->validation_errors;
				$this->load->view('users/add', $viewdata);
			}
		//if not post, show form
		}else{
			$this->load->view('users/add');
		}
	}
	
	public function edit($id){
		$postdata = $this->input->post();
		// If POST, edit user
		if($postdata){
			
		}else{
			$viewdata = array();
			$viewdata['message'] = $this->session->flashdata('message');
			$viewdata['user'] = $this->user->find_by_id($id);
			bound_form('user', 'users/edit', array('legend'=>'Edit user', 'submit_value' => 'Save', 'print' => false), $viewdata['user'] );
			bound_form_remove(3);
			bound_form_remove(3);
			$this->load->view('users/edit', $viewdata);
		}
	}
	
	public function delete(){
	
	}
	
	public function login(){
		$postdata = $this->input->post();
		if( $postdata  ){
			if( $this->login->login($postdata['email'], $postdata['password']) ){
				$this->redirect('users/');
			}else{
				bound_form('login', 'users/login/', array('print'=>false) );
				$viewdata['errors'] = $this->login->validation_errors;
				$this->load->view('users/login', $viewdata);
			}
		}else{
			$viewdata = array();
			$viewdata['message'] = $this->session->flashdata('message');
			$redirect = $this->session->flashdata('login_redirect');
			bound_form('login', 'users/login/', array('print'=>false), array('value' => 'Log In') );
			bound_form_add( Form::hidden('login_redirect', $redirect), 0);
			$this->load->view('users/login', $viewdata);
		}
		
		
	}
	
	public function logout(){
		$this->login->logout();
		$this->session->set_flashdata('message', 'You are now logged out');
		$this->redirect('users/login');
	}
	
	public function me(){
		$viewdata['user'] = $this->login->current_user();
		bound_form('user', 'users/edit', array('legend'=>'Edit user', 'submit_value' => 'Save', 'print' => false), $viewdata['user'] );
		bound_form_remove(3);
		bound_form_remove(3);
		$this->load->view('users/edit', $viewdata);
	}
	
	public function change_password($id=false, $token = false){
		if($id===false)return;
		$postdata = $this->input->post();
		if($postdata){
			if( trim($postdata['new_password']) != '' && $postdata['new_password_confirm'] == $postdata['new_password'] ){
				$result = $this->user->reset_password($postdata['email'], $postdata['token'], $postdata['new_password'] );
				if(!$result){
					echo $this->user->error_message;	
				}else{
					if($this->login->is_logged_in() ){
						$this->session->set_flashdata('message', 'Password changed');
						$this->redirect('users/edit/'.$id);
					}else{
							
					}
				}
			}else{
				echo "Required data missing";	
			}
		}elseif($token){
			$user = $this->user->find_by_reset_token($token);
			if(!$user){
				echo "Invalid Token";	
			}else{
				$viewdata = array('user' => $user, 'token' => $token);
				$this->load->view('users/reset_password', $viewdata);
			}
		}else{
			$user = $this->user->find_by_id($id);
			$token = $this->user->reset_password_request($user['email']);
			echo $token;
		}
	}
	
	function forgotten_password(){
		$postdata = $this->input->post();
		if($postdata){
			
		}else{
			
		}
	}


}

?>